package com.friend.joey.applicationfriend;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;

/**
 * Created by Joey on 2017/5/28.
 */

public class ActivityForTest extends AppCompatActivity {
    private TextView textView;
    private int total=-1;
    private TraceService.TraceServiceBinder traceBinder;
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            traceBinder=(TraceService.TraceServiceBinder)iBinder;
            //traceBinder.startTrace();
            total=traceBinder.getProgress();
            traceBinder.setTraceServiceListener(new TraceService.TraceServiceListener() {
                @Override
                public void onBack(int state, int status,String s) {
                    Toast.makeText(ActivityForTest.this,s,Toast.LENGTH_LONG).show();
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Toast.makeText(ActivityForTest.this,"关闭连接",Toast.LENGTH_LONG).show();
        }
    };

    private MyHandler mHandler;
    private class MyHandler extends Handler {
        private WeakReference<ActivityForTest> mWeakReference;
        public MyHandler(ActivityForTest activity){
            mWeakReference=new WeakReference<ActivityForTest>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ActivityForTest activity= mWeakReference.get();
            if(activity!=null){
                switch(msg.what){
                    case 1:
                        //Toast.makeText(ActivityForTest.this,msg.obj.toString(),Toast.LENGTH_SHORT).show();
                        textView.setText(msg.obj.toString());
                        break;
                    case 2:
                        Toast.makeText(activity,msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    case -1:
                        //Toast.makeText(activity, "失败", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(activity, "没有信息", Toast.LENGTH_SHORT).show();
                }
            }
            else{
                Toast.makeText(activity, "错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait);
        mHandler = new MyHandler(this);
        Button button=(Button)findViewById(R.id.buttonfortest1);
        Button button2=(Button)findViewById(R.id.buttonfortest2);
        Button button3=(Button)findViewById(R.id.buttonfortest3);
        Button button4=(Button)findViewById(R.id.buttonfortest4);
        textView=(TextView)findViewById(R.id.textfortest);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent bindIntent = new Intent(ActivityForTest.this,TraceService.class);
                bindService(bindIntent,connection,BIND_AUTO_CREATE);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent startIntent=new Intent(ActivityForTest.this,TraceService.class);
                startIntent.putExtra("entityName","myTrace");
                startService(startIntent);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //total=traceBinder.getProgress();

                Intent stopIntent=new Intent(ActivityForTest.this,TraceService.class);
                stopService(stopIntent);
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //unbindService(connection);
                //traceBinder.stopTrace();
                unbindService(connection);
                //Toast.makeText(ActivityForTest.this,Integer.toString(total),Toast.LENGTH_LONG).show();
            }
        });

    }


}
